package com.hilon.foundation.simplest;


import com.hilon.foundation.base.BaseServiceImpl;
import com.hilon.foundation.util.ResultBean;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

@Service
public abstract class BaseSimplestServiceImpl<T extends EntityBaseSimplest,V>
        extends BaseServiceImpl<T,V> {

    public BaseSimplestServiceImpl(EntityManager entityManager
                                   ) {
        super(entityManager);
    }
    @Transactional
    public ResultBean<String> append(T entity) {
        String sql = "select e from " + entityName +
                " e " +
                "where e.no=?1 ";
        try {
            Query query = entityManager.createQuery(sql);
            query.setParameter(1, entity.getNo());
            int num=query.getResultList().size();
            if(num==0)
            {
                entityManager.persist(entity);
                return new ResultBean<>(200,"成功增加记录");
            }
            else
                return new ResultBean<>(501,"增加记录失败-记录已经存在");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ResultBean<>(501,"增加记录失败");
        }
    }
}
